<!-- 评估分析工具_统计分析 -->
<template>
    <div class="statistic-analysis-container">
        <div class="s-a-main flex flex-row">
            <div class="s-a-left flex flex-col">
                <Tree ref="statisticAnalysisDataTreeRef" :isGeneralFolderAndFile="true" :topAdd="true"
                    :defaultExpandedKeys="defaultExpandedKeys" :currentNodeKey="currentNodeKey"
                    :treeData="statisticAnalysisList" :topAddMenu="right_menuList.rightMenuList_1"
                    :folderMenu="right_menuList.rightMenuList_5" :fileMenu="right_menuList.rightMenuList_5"
                    @handleAddNode="handleAddIndexSystem" @handleMenuClick="handleMenuClick" @nodeClick="nodeClick"
                    @moveNodeOrder="moveNodeOrder"></Tree>
            </div>
            <div class="s-a-divider"></div>
            <div class="s-a-right">
                <FolderDetail v-if="isFolder" ref="folderRef" :nodeInfo="selectedNodeInfo">
                </FolderDetail>
                <StatisticAnalysisDetail v-if="isFile" ref="statisticAnalysisDetailRef" :nodeInfo="selectedNodeInfo"
                    @refresh="getStatisticAnalysisTree"></StatisticAnalysisDetail>
            </div>
        </div>
    </div>
</template>

<script>
import {
    getSelfGlobalDataTree,
    deleGlobalData,
    globalDataMoveTreeNode,
} from "@/api/myspace/model-manage-tool/model-manage";
import { getStatisticAnalysisTree } from "@/api/myspace/evaluation-analysis-tool/evaluation-analysis";
import { mapState } from "vuex";
import Tree from "@/components/LeftTree/tree.vue";
import FolderDetail from "./components/statistic-analysis/folder-detail.vue";
import StatisticAnalysisDetail from "./components/statistic-analysis/statistic-analysis-detail.vue";
export default {
    name: "evaluationIndex",
    data() {
        return {
            queryContent: "",
            activeName: null,
            mainVersionInfo: null,
            isAddOrUpdateFolder: false,
            isFolder: false, // 是否展示文件夹详情
            isFile: false, // 是否展示文件详情
            selectedNodeInfo: {}, // 当前选中节点的信息
            statisticAnalysisList: [], // 指标体系列表
            defaultExpandedKeys: [], //树默认要展开的节点
            currentNodeKey: null,
            isFilterOpen: false,
            filters: {
                status: "",
                tags: "",
                owner: "",
                creator: "",
                lastModifier: "",
                matchType: "include",
            },
        };
    },
    components: {
        Tree,
        FolderDetail,
        StatisticAnalysisDetail
    },
    computed: {
        ...mapState({
            right_menuList: (state) => state.evaluationAnalysisTool.right_menuList,
            userId: (state) => state.user.id,
            selectedNodeId: (state) => state.evaluationAnalysisTool.analysis.selectedNodeId,
            expandedNodeIds: (state) => state.evaluationAnalysisTool.analysis.expandedNodeIds,
        }),
        filterNum() {
            return Object.values(this.filters).filter((item) => item).length - 1;
        },
    },
    async created() {
        await this.getStatisticAnalysisTree();
        this.currentNodeKey = this.selectedNodeId;
        this.defaultExpandedKeys = this.expandedNodeIds;
        this.$nextTick(() => {
            this.$refs.statisticAnalysisDataTreeRef.$refs.treeRef.setCurrentKey(this.currentNodeKey)
        })
    },
    mounted() {
    },
    methods: {
        async getStatisticAnalysisTree(newNodeId) {
            await getStatisticAnalysisTree().then((response) => {
                if (response.code === 200) {
                    this.statisticAnalysisList = response.data[0].commonTreeList;
                    this.nodeClick(
                        newNodeId ? this.recurNodeForTarget(this.statisticAnalysisList, newNodeId) :
                            this.selectedNodeId ? this.recurNodeForTarget(this.statisticAnalysisList, this.selectedNodeId) :
                                this.statisticAnalysisList[0])
                }
            });
        },

        getSelfGlobalDataTree(flag = false, newNodeId) {
            getSelfGlobalDataTree().then((response) => {
                if (response.code == 200) {
                    if (Array.isArray(response.data)) {
                        this.modelDataList = response.data[0].commonTreeList;
                        localStorage.setItem(
                            "globalTreeData",
                            JSON.stringify(this.modelDataList)
                        );
                        if (flag) {
                            this.defaultExpandedKeys = [this.modelDataList[0].id]
                            this.currentNodeKey = this.modelDataList[0].id
                            this.$nextTick(() => {
                                this.$refs.modelDataTreeRef.$refs.treeRef.setCurrentKey(this.currentNodeKey)
                            })
                        } else {
                            let node = this.recurNodeForTarget(
                                this.modelDataList,
                                newNodeId ? newNodeId : this.selectedNodeInfo.id
                            );
                            node = node ? node : this.modelDataList[0];
                            this.selectedNodeInfo = node
                            this.defaultExpandedKeys = Array.from(new Set([...this.defaultExpandedKeys, node.parentId]));
                            this.currentNodeKey = node.id;
                            this.$nextTick(() => {
                                this.$refs.modelDataTreeRef.$refs.treeRef.setCurrentKey(this.currentNodeKey)
                            })
                        }
                    }
                }
            });
        },
        // 递归获取当前树节点
        recurNodeForTarget(nodeArr, id) {
            for (let i = 0; i < nodeArr.length; i++) {
                if (nodeArr[i].id == id) {
                    return nodeArr[i];
                } else {
                    if (nodeArr[i].children && nodeArr[i].children.length > 0) {
                        let node = this.recurNodeForTarget(nodeArr[i].children, id);
                        if (node) {
                            return node;
                        }
                    }
                }
            }
        },
        //显示筛选条件
        openFilter() {
            this.isFilterOpen = !this.isFilterOpen;
        },
        // 点击菜单项触发的事件
        handleMenuClick(menu, node) {
            switch (menu.name) {
                case "新增分类":
                    this.handleAddOrUpdateFolder("add", node);
                    break;
                case "新增指标体系":
                    this.handleAddIndexSystem(node);
                    break;
                case "添加子目录":
                    this.handleAddOrUpdateFolder("add", node);
                    break;
                case "编辑":
                    this.handleAddOrUpdateFolder("edit", node);
                    break;
            }
        },
        // 新增指标体系
        handleAddIndexSystem(data) {
            console.log("新增指标体系", data);
            let rep = data ? data : { id: 0, data: {} }
            this.selectedNodeInfo = rep;
            this.isFolder = false;
            this.isFile = true;
            this.$nextTick(() => {
                // this.$refs.jsonSchemaRef.isAddModelData = true;
                // this.$refs.jsonSchemaRef.reset(true, this.selectedNodeInfo.data);
            });
        },
        // 新增或修改文件夹
        handleAddOrUpdateFolder(sign, node) {
            // this.isAddOrUpdateFolder = true;
            // this.$nextTick(() => {
            //     this.$refs.addOrUpdateFolderRef.init(
            //         sign,
            //         node,
            //         this.modelDataList,
            //         "global",
            //     );
            // });
        },
        // 删除文件夹或文件
        handleDeleteFile(node) {
            this.$confirm(`确定删除目录  ${node.name} ?`, "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                customClass: "message-box-style",
                type: "warning",
            })
                .then(() => {
                    deleGlobalData(
                        node.id
                    ).then((response) => {
                        if (response.code == 200) {
                            this.$message.success(
                                this.$toolHandle.getI18nName(
                                    this.$zh.setup.responseText,
                                    this.$en.setup.responseText,
                                    this.$i18n.locale,
                                    "success"
                                )
                            );
                            this.isFolder = false;
                            this.selectedNodeInfo = {};
                            this.$refs.modelDataTreeRef.deleteNode();
                            this.getSelfGlobalDataTree();
                        }
                    });
                })
                .catch(() => { });
        },
        nodeClick(node) {
            if (node == undefined) return;
            this.selectedNodeInfo = node;
            this.currentNodeKey = node.id;
            this.$nextTick(() => {
                this.$refs.statisticAnalysisDataTreeRef.$refs.treeRef.setCurrentKey(this.currentNodeKey)
            })
            this.$store.commit("evaluationAnalysisTool/SET_SELECTED_NODE_ID", { moduleName: "analysis", id: node.id })
            if (node.type == 0) {
                // 点击的文件夹
                this.isFolder = true;
                this.isFile = false;
                this.$store.commit("evaluationAnalysisTool/SET_EXPANDED_NODE_IDS", { moduleName: "analysis", idList: Array.from(new Set([...this.defaultExpandedKeys, node.id])) })
            } else {
                // 点击的文件
                this.isFolder = false;
                this.isFile = true;
            }
        },
        // 移动树节点的顺序
        moveNodeOrder(draggingNode, dropNode, dropType) {
            globalDataMoveTreeNode({
                movedNodeId: draggingNode.data.id, // 被移动节点的id
                referenceNodeId: dropNode.data.id, // 参照节点的id
                position: dropType == "after" ? 1 : dropType == "before" ? -1 : 0, // 相对位置，before:-1,into:0,after:1
                ancestors: draggingNode.data.data.ancestors, // 祖先
                folderChildrenOrder: {
                    parentId:
                        dropType == "inner" ? dropNode.data.id : dropNode.data.parentId, // 父级id
                    siblingIds:
                        dropType == "inner"
                            ? Array.from(dropNode.childNodes).map((item) => item.data.id)
                            : Array.from(dropNode.parent.childNodes).map(
                                (item) => item.data.id
                            ),
                },
            }).then((response) => {
                if (response.code == 200) {
                    this.$message.success(
                        this.$toolHandle.getI18nName(
                            this.$zh.setup.responseText,
                            this.$en.setup.responseText,
                            this.$i18n.locale,
                            "success"
                        )
                    );
                    this.getSelfGlobalDataTree();
                }
            });
        },
        // 使用自定义数据，弹框，选择模型，选择版本，选择数据结构
        handleUseCustomData(node) {
            this.isUseCustomData = true;
            this.$nextTick(() => {
                this.$refs.useCustomDataRef.init();
            });
        },
        // 导入jsonschema文件
        handleImportFile(node) {
            this.selectedNodeInfo = node;
        },
    },
};
</script>

<style lang="scss" scoped>
.mm-l-tree-header {
    height: 40px;
    line-height: 40px;
    padding-left: 8px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: var(--ui-text-color);
    border-bottom: 1px solid var(--ui-border-color-split);

    .icon-box {
        height: 32px;
        visibility: hidden;
    }

    &:hover {
        .icon-box {
            visibility: visible;

            .item-icon {
                width: 32px;
                height: 100%;

                &:hover {
                    background-color: var(--ui-btn-text-hover-bg);
                    border-radius: 6px;
                }
            }
        }
    }
}

.statistic-analysis-container {
    width: 100%;
    height: 100%;
    padding: 0 10px;
    box-sizing: border-box;

    .s-a-main {
        width: 100%;
        height: 100%;

        .s-a-left {
            width: 300px;
            height: 100%;
            flex-grow: 0;
            flex-shrink: 0;
            display: flex;
            padding: 10px 10px 0 0;
            overflow: auto;

            .filer-box {
                display: flex;
                flex-wrap: wrap;
                transition: all 0.5s;

                .filter-item {
                    width: 50%;
                    padding: 5px 5px 0 0;

                    .el-radio-group {
                        display: flex;
                    }
                }
            }

            .tree-box {
                flex-grow: 1;
                margin-top: 5px;
                box-sizing: border-box;
            }
        }

        .s-a-divider {
            width: 1px;
            height: 100%;
            flex-grow: 0;
            align-self: stretch;
            background-color: var(--ui-border-color-split);
        }

        .s-a-right {
            width: calc(100% - 300px);
            height: 100%;
            min-width: 0px;
        }
    }
}
</style>